<?php

namespace IMT\AccountBundle\Entity;

use IMT\AccountBundle\Models\AccountInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
 * IMT\AccountBundle\Entity\AccountIMT
 *
 * @ORM\Table(name="account_type_gmail")
 * @ORM\Entity
 */
class AccountGmail extends Account
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
	protected $type = 'gmail';
    
    /**
     * @var string $access_token
     *
     * @ORM\Column(name="access_token", type="string", length=255, nullable=true)
     */
    protected $access_token;
    
    /**
     * @var string $refresh_token
     *
     * @ORM\Column(name="refresh_token", type="string", length=255, nullable=true)
     */
    protected $refresh_token;
    
    /**
     * @var string $expire_in
     *
     * @ORM\Column(name="expire_in", type="string", length=255, nullable=true)
     */
    protected $expire_in;
    
    /**
     * @var string $last_update
     *
     * @ORM\Column(name="last_update", type="datetime", nullable=true)
     */
    protected $last_update;   

    /**
    * @var boolean $active_synch
    *
    * @ORM\Column(name="active_synch", type="boolean", nullable=true)
    */
    protected $active_synch;
    
    /**
	 * @return the $access_token
	 */
	public function getAccessToken() {
		$now = new \DateTime();
		
		if(
				$this->access_token && $this->expire_in &&
				$this->last_update instanceof \DateTime &&
				intval($this->last_update->format('U'))+$this->expire_in >= intval($now->format('U'))+50
		)
			return $this->access_token;
		
		return null;
	}

	/**
	 * @return the $refreshToken
	 */
	public function getRefreshToken() {
		if($this->refresh_token) {
    		return $this->refresh_token;
    	}

    	return null;
	}

	/**
	 * @return the $active_synch
	 */
	public function getActiveSynch() {
		return $this->active_synch;
	}

	/**
	 * @param boolean $active_synch
	 */
	public function setActiveSynch($active_synch) {
		$this->active_synch = $active_synch;
	}

	/**
	 * @return the $expire_in
	 */
	public function getExpireIn() {
		return $this->expire_in;
	}

	/**
	 * @return the $last_update
	 */
	public function getLastUpdate() {
		return $this->last_update;
	}

	/**
	 * @param string $accessToken
	 */
	public function setAccessToken($access_token) {
		$this->access_token = $access_token;
		
		$this->last_update = new \DateTime();
	}

	/**
	 * @param string $refreshToken
	 */
	public function setRefreshToken($refresh_token) {
		$this->refresh_token = $refresh_token;
	}

	/**
	 * @param string $expire_in
	 */
	public function setExpireIn($expire_in) {
		$this->expire_in = $expire_in;
	}

	/**
	 * @param string $last_update
	 */
	public function setLastUpdate($last_update) {
		$this->last_update = $last_update;
	}

	public function getName()
    {
    	return 'Gmail';
    }
    
    public function getType()
    {
    	return 'gmail';
    }
    
    public function setType($type)
    {
    	$this->type = 'gmail';
    }
    
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}